package com.servlet.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import DB.DBManager;

import com.servlet.vo.User;

public class UserDao {

	private Connection con;
	private Statement statement;
	private ResultSet resultSet;
	private boolean flag = false;
	private StringBuffer sql = new StringBuffer();
	
	public UserDao() throws SQLException{
		con = DBManager.getConn();
		statement = con.createStatement();
	}
	public void close(){
		
		if(null != resultSet){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != statement){
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean save(User obj) {
		
		sql.append("INSERT INTO USER(ADDTIME,deleteStatus,userName,password,trueName) VALUES( NOW(),FALSE,'");
		sql.append(obj.getUserName() + "','" + obj.getPassword() + "','"+obj.getTrueName() + "')");
		
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = true;
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean delete(Long id){
		
		sql.append("UPDATE user SET deleteStatus = TRUE WHERE id = '" + id + "'");
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean update(User obj){
		
		try {
			sql.append("UPDATE USER set ");
/*		if(obj.getUserName() != null){
				sql.append("userName,");
			}*/
			if(obj.getPassword() != null){
				sql.append("password='"+obj.getPassword()+"',");
			}
			if(obj.getTrueName() != null){
				sql.append("trueName='"+obj.getTrueName()+"',");
			}
			if(sql.toString().endsWith(",")){
				sql.deleteCharAt(sql.toString().length()-1);
				sql.append(" where id = "+obj.getId());
					
				try {
					flag = statement.executeUpdate(sql.toString()) == 1;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public List<User> getList(){
		
		sql.append("SELECT id,userName,password,trueName,acc_id FROM user WHERE deleteStatus = FALSE ");
		List<User> objs = new ArrayList<User>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			User obj = null;
			while(resultSet.next()){
			
				obj = new User();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getDate("addTime"));
				obj.setUserName(resultSet.getString("userName"));
				obj.setTrueName(resultSet.getString("trueName"));
				obj.setPassword(resultSet.getString("password"));
				obj.setAcc(resultSet.getLong("acc_id"));
				objs.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return objs;
	}
	
	/**
	 * 获取登陆用户方法
	 * @param userName
	 * @param passwd
	 * @return
	 */
	public User getLoadUser(String userName, String passwd){
	
		sql.append("SELECT id,addTime,userName,password,trueName,acc_id FROM user WHERE deleteStatus = FALSE ");
		sql.append("and userName = '" + userName + "' ");
		sql.append("and password = '" + passwd + "' ");
		User obj = null;
		try {
			resultSet = statement.executeQuery(sql.toString());
			if(resultSet.next()){
				obj = new User();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getDate("addTime"));
				obj.setUserName(resultSet.getString("userName"));
				obj.setTrueName(resultSet.getString("trueName"));
				obj.setPassword(resultSet.getString("password"));
				obj.setAcc(resultSet.getLong("acc_id"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return obj;
	}
}